package com.qclix.inv.webui;

import com.qclix.inv.Client;
import com.qclix.inv.InvoiceService;
import com.qclix.inv.webui.jsonobj.JSonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/client")
public class ClientController {
    private static Logger logger = LoggerFactory.getLogger(ClientController.class);

    @Autowired
    private InvoiceService invoiceService;

    public InvoiceService getInvoiceService() {
        return invoiceService;
    }

    public void setInvoiceService(InvoiceService invoiceService) {
        this.invoiceService = invoiceService;
    }

    @RequestMapping(value = "/autocomplete", method = RequestMethod.GET)
    public
    @ResponseBody
    JSonClient[] getClientThatContains(HttpServletRequest request, HttpServletResponse response) {
        List<Client> clients = null;
        try {
            clients = invoiceService.getClients();
        } catch (Throwable e) {
            logger.error("Could not retrieve the clients.", e);
            return null;
        }

        List<Client> matchingClientList = new ArrayList<Client>();
        String pattern = request.getParameter("term");
        if (pattern != null) {
            pattern = pattern.toUpperCase();
            for (Client client : clients) {
                if (client.getName().toUpperCase().contains(pattern)) {
                    matchingClientList.add(client);
                }
            }
        }

        JSonClient[] matchingClients = new JSonClient[matchingClientList.size()];
        for (int i = 0; i < matchingClientList.size(); i++) {
            matchingClients[i] = JSonObjTranslator.getJSon(matchingClientList.get(i));
        }
        return matchingClients;
    }

    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, String> newClient(@RequestBody JSonClient client, HttpServletResponse response) {
        Client domainClient = JSonObjTranslator.getModel(client);
        logger.debug("Creating " + domainClient);
        try {
            invoiceService.addClient(domainClient);
        } catch (Throwable e) {
            logger.error("Error saving " + domainClient, e);
            return null;
        }
        logger.info(domainClient.toString() + " created");
        Map<String, String> returnMap = new HashMap<String, String>();
        returnMap.put("id", "" + domainClient.getId());
        return returnMap;
    }
}
